import { QINIU_CONFIG } from './qiniuUpload';
import prompt from '@ohos.promptAction';

/**
 * 验证七牛云配置
 * @returns 配置验证结果
 */
export function validateQiniuConfiguration(): {
  isValid: boolean;
  errors: string[];
  warnings: string[];
} {
  const errors: string[] = [];
  const warnings: string[] = [];

  // 检查AccessKey
  if (!QINIU_CONFIG.accessKey || QINIU_CONFIG.accessKey === 'your_access_key') {
    errors.push('AccessKey未配置或使用默认值');
  } else if (QINIU_CONFIG.accessKey.length < 10) {
    warnings.push('AccessKey长度可能不正确');
  }

  // 检查SecretKey
  if (!QINIU_CONFIG.secretKey || QINIU_CONFIG.secretKey === 'your_secret_key') {
    errors.push('SecretKey未配置或使用默认值');
  } else if (QINIU_CONFIG.secretKey.length < 10) {
    warnings.push('SecretKey长度可能不正确');
  }

  // 检查Bucket
  if (!QINIU_CONFIG.bucket || QINIU_CONFIG.bucket === 'your_bucket_name') {
    errors.push('Bucket未配置或使用默认值');
  }

  // 检查Domain
  if (
    !QINIU_CONFIG.domain ||
    QINIU_CONFIG.domain === 'https://your-domain.com'
  ) {
    errors.push('Domain未配置或使用默认值');
  } else if (!QINIU_CONFIG.domain.startsWith('http')) {
    errors.push('Domain必须以http或https开头');
  }

  // 检查UploadUrl
  if (
    !QINIU_CONFIG.uploadUrl ||
    QINIU_CONFIG.uploadUrl === 'https://upload-z2.qiniup.com'
  ) {
    errors.push('UploadUrl未配置或使用默认值');
  } else if (!QINIU_CONFIG.uploadUrl.startsWith('https://upload')) {
    warnings.push('UploadUrl格式可能不正确');
  }

  return {
    isValid: errors.length === 0,
    errors,
    warnings,
  };
}

/**
 * 显示配置验证结果
 */
export function showConfigurationValidation(): void {
  const result = validateQiniuConfiguration();

  if (result.isValid) {
    prompt.showToast({
      message: '七牛云配置验证通过',
      duration: 2000,
    });
    console.log('✅ 七牛云配置验证通过');
  } else {
    const errorMessage = result.errors.join(', ');
    prompt.showToast({
      message: `配置错误: ${errorMessage}`,
      duration: 4000,
    });
    console.error('❌ 七牛云配置验证失败:', result.errors);
  }

  if (result.warnings.length > 0) {
    console.warn('⚠️ 配置警告:', result.warnings);
  }
}

/**
 * 获取配置信息（用于调试）
 */
export function getConfigurationInfo(): void {
  console.log('=== 七牛云配置信息 ===');
  console.log('AccessKey:', QINIU_CONFIG.accessKey.substring(0, 8) + '...');
  console.log('SecretKey:', QINIU_CONFIG.secretKey.substring(0, 8) + '...');
  console.log('Bucket:', QINIU_CONFIG.bucket);
  console.log('Domain:', QINIU_CONFIG.domain);
  console.log('UploadUrl:', QINIU_CONFIG.uploadUrl);
  console.log('========================');
}
